﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Module.Core.Common;
using Module.Core.EntitiesInfo.TablesInfo;
using Module.Core.LoginSystem.Commons.Utilities;

namespace Web
{
    public partial class addCookies : System.Web.UI.Page
    {
        string kqid = "";
        string kqnum = "";
        
           
           

        protected void Page_Load(object sender, EventArgs e)
        {
            HttpCookie PRO_ID_COOKIE = HttpContext.Current.Request.Cookies.Get(ComomFunctions.PRO_ID_COOKIE);
            HttpCookie PRO_NUM_COOKIE = HttpContext.Current.Request.Cookies.Get(ComomFunctions.PRO_NUM_COOKIE);
            HttpCookie cookie = HttpContext.Current.Request.Cookies.Get("setvalue");
            string id = Request.QueryString["id"];

            if (cookie == null)
            {
                // Check if cookie exists in the current request.                                
                cookie = new HttpCookie("setvalue");
                cookie.Value = "1";
                cookie.Expires = DateTime.Now.AddMinutes(10);
                Response.Cookies.Add(cookie);
            }
            else
            {
                string count = "";
                // Check if cookie exists in the current request.           
                count = cookie.Value;
                count = count + "|" + id;
                cookie.Value = count.ToString();
                cookie.Expires = DateTime.Now.AddMinutes(10);
                Response.Cookies.Add(cookie);
            }

            //get cookies

            
            AddCartCookies();
            if (PRO_ID_COOKIE != null)
            {
                Response.Write("aaa:" + PRO_ID_COOKIE.Value + " sl:" + PRO_NUM_COOKIE.Value);                
            }
        }

        void AddCartCookies()
        {                     
            int productID = int.Parse(Request.QueryString["id"].ToString());
            //string keyword = Request.QueryString["keyword"].ToString();
            //if (Request.QueryString["keyword"].ToString() != "")
            //{
            //    keyword = keyword.Replace(".aspx", "");
            //}
            //ProductsInfo productsInfo = ProductsBL.SelectFollowKeywors(keyword, 1);
            //if (productsInfo != null)
            //{
            //    productID = productsInfo.ProductId;
            //}

            if (productID > 0)
            {
                Response.Clear();
                bool kt = false;
                if (Request.Cookies[ComomFunctions.PRO_ID_COOKIE] != null)
                {
                    string[] array_id = Request.Cookies[ComomFunctions.PRO_ID_COOKIE].Value.Split('n');
                    string[] array_num = Request.Cookies[ComomFunctions.PRO_NUM_COOKIE].Value.Split('n');

                    for (int i = 0; i < array_id.Length; i++)
                    {
                        if (ConvertUtility.ToInt32(array_id[i]) == productID)
                        {
                            int chan = ConvertUtility.ToInt32(array_num[i]);
                            chan++;
                            array_num[i] = chan.ToString();
                            kt = true;
                        }
                    }

                    for (int i = 0; i < array_id.Length; i++)
                    {
                        if (kqid == "")
                        {
                            kqid = array_id[i];
                        }
                        else
                        {
                            kqid = kqid + "n" + array_id[i];
                        }

                        if (kqnum == "")
                        {
                            kqnum = array_num[i];
                        }
                        else
                        {
                            kqnum = kqnum + "n" + array_num[i];
                        }


                    }

                    if (kt)
                    {
                        ComomFunctions.DelCookie();
                        HttpCookie PRO_ID_COOKIE = HttpContext.Current.Request.Cookies.Get(ComomFunctions.PRO_ID_COOKIE);
                        HttpCookie PRO_NUM_COOKIE = HttpContext.Current.Request.Cookies.Get(ComomFunctions.PRO_NUM_COOKIE);

                        //PRO_ID_COOKIE = new HttpCookie("ComomFunctions.PRO_ID_COOKIE");
                        //PRO_NUM_COOKIE = new HttpCookie("ComomFunctions.PRO_NUM_COOKIE");

                        PRO_ID_COOKIE.Value = kqid;
                        PRO_ID_COOKIE.Expires = DateTime.Now.AddYears(30);
                        Response.Cookies.Add(PRO_ID_COOKIE);
                        
                        PRO_NUM_COOKIE.Value = kqnum;
                        PRO_NUM_COOKIE.Expires = DateTime.Now.AddYears(30);
                        Response.Cookies.Add(PRO_NUM_COOKIE);

                    }
                    else
                    {
                        ComomFunctions.DelCookie();
                        HttpCookie PRO_ID_COOKIE = HttpContext.Current.Request.Cookies.Get(ComomFunctions.PRO_ID_COOKIE);
                        HttpCookie PRO_NUM_COOKIE = HttpContext.Current.Request.Cookies.Get(ComomFunctions.PRO_NUM_COOKIE);

                        //PRO_ID_COOKIE = new HttpCookie("ComomFunctions.PRO_ID_COOKIE");
                        //PRO_NUM_COOKIE = new HttpCookie("ComomFunctions.PRO_NUM_COOKIE");

                        PRO_ID_COOKIE.Value = kqid + "n" + productID.ToString(); 
                        PRO_ID_COOKIE.Expires = DateTime.Now.AddYears(30);
                        Response.Cookies.Add(PRO_ID_COOKIE);                        

                        PRO_NUM_COOKIE.Value = kqnum + "n1";
                        PRO_NUM_COOKIE.Expires = DateTime.Now.AddYears(30);
                        Response.Cookies.Add(PRO_NUM_COOKIE);                        
                    }
                }
                else
                {
                    ComomFunctions.DelCookie();

                    HttpCookie PRO_ID_COOKIE = HttpContext.Current.Request.Cookies.Get(ComomFunctions.PRO_ID_COOKIE);
                    HttpCookie PRO_NUM_COOKIE = HttpContext.Current.Request.Cookies.Get(ComomFunctions.PRO_NUM_COOKIE);

                    //PRO_ID_COOKIE = new HttpCookie("ComomFunctions.PRO_ID_COOKIE");
                    //PRO_NUM_COOKIE = new HttpCookie("ComomFunctions.PRO_NUM_COOKIE");

                    PRO_ID_COOKIE.Value = productID.ToString();
                    PRO_ID_COOKIE.Expires = DateTime.Now.AddYears(30);
                    Response.Cookies.Add(PRO_ID_COOKIE);
                    
                    PRO_NUM_COOKIE.Value = "1";
                    PRO_NUM_COOKIE.Expires = DateTime.Now.AddYears(30);
                    Response.Cookies.Add(PRO_NUM_COOKIE);
                }
            }
        }
    }
}